//
//  PerfectInfoController.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/1/20.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "PerfectInfoController.h"
#import "BRPickerView.h"
#import "AddressProvinceCityController.h"
#import "SuccessToOriginController.h"

#define NUMBERS @"0123456789"
@interface PerfectInfoController ()<UITextFieldDelegate>

//真实姓名
@property (weak, nonatomic) IBOutlet UITextField *realNameTextFild;

//性别
@property (weak, nonatomic) IBOutlet UIView *sexView;
//年龄
@property (weak, nonatomic) IBOutlet UIView *ageView;
//所在地
@property (weak, nonatomic) IBOutlet UIView *locationView;

//性别textfield
@property (weak, nonatomic) IBOutlet UITextField *sexTextField;
//年龄textfield
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
//所在地textfield
@property (weak, nonatomic) IBOutlet UITextField *locationTextField;
//医生类型
@property (weak, nonatomic) IBOutlet UIButton *doctorTypeButton;
//求美者类型
@property (weak, nonatomic) IBOutlet UIButton *counselorTypeButton;
//注册
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

/** 选择省份 */
@property (nonatomic, copy) NSString *seltedProvince;
/** 选择城市 */
@property (nonatomic, copy) NSString *seltedCity;

@end

@implementation PerfectInfoController

- (void)dealloc {
    NSLog(@"------>llq PerfectInfoController dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"完善资料";
    
    NSArray *tapViews = @[self.sexView, self.ageView, self.locationView];
    for (UIView *tapView in tapViews) {
        [tapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewDidTap:)]];
    }
    
    self.ageTextField.delegate = self;
    
    NSArray *btns = @[self.doctorTypeButton, self.counselorTypeButton, self.confirmButton];
    for (UIButton *btn in btns) {
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - Action
- (void)tapViewDidTap:(UITapGestureRecognizer *)tap {
    UIView *tapView = tap.view;
    
    if (tapView == self.sexView) {
        [self p_showSexPicker];
    }else if (tapView == self.ageView) {
//        [self p_showAgePicker];
    }else if (tapView == self.locationView) {
        [self p_showLocationList];
    }
}

- (void)btnDidClick:(UIButton *)sender {
    if (sender == self.doctorTypeButton) {
        sender.selected = !sender.isSelected;
        self.counselorTypeButton.selected = !sender.isSelected;
        
    }else if (sender == self.counselorTypeButton) {
        sender.selected = !sender.isSelected;
        self.doctorTypeButton.selected = !sender.isSelected;
    
    }else if (sender == self.confirmButton) {
        NSString *validate = [self p_validateIsCanRegister];
        if (validate.length != 0) {
            [KLProgressHUD showPromptHUDWithMessage:validate];
            return;
        }
        
        [self request_register];
    }
}

#pragma mark ----uitextFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSCharacterSet *cs;
    if(textField == self.ageTextField)
    {
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest)
        {
            [KLProgressHUD showErrorHUDWithMessage:@"只能输入数字"];
            //输入了非法字符
            return NO;
        }
    }
    
    //其他的类型不需要检测，直接写入
    return YES;
}


#pragma mark - Request
- (void)request_register {

    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    
    if (self.ageTextField.text.length != 0) {
        
        if (self.ageTextField.text.length !=4 ||(![[self.ageTextField.text substringWithRange:NSMakeRange(0,2)] isEqualToString:@"19"]&&![[self.ageTextField.text substringWithRange:NSMakeRange(0,2)] isEqualToString:@"20"])) {
            [KLProgressHUD showErrorHUDWithMessage:@"请输入正确年份"];
            return;
        }
    }
    param[@"age"] = self.ageTextField.text;

    param[@"city"] = self.seltedCity;
    param[@"province"] = self.seltedProvince;
    param[@"passWord"] = [NSString app_md5:self.pwd];
    param[@"phone"] = self.phone;
    param[@"realName"] = self.realNameTextFild.text;
    param[@"sex"] = self.sexTextField.text;
    param[@"smsCode"] = self.smsCode;
    param[@"type"] = self.doctorTypeButton.selected? @"1": @"2";
    
    KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:@"注册中.."];
    
    [AppNetwork postWithUrl:kRegisterAccountUrl params:param success:^(id response) {
        progressHUD.hidden = YES;
        
        [AppNotificationHelpper postNotificationName:@"_clearAccountAndPwd_" object:nil];
        
        SuccessToOriginController *success = [[SuccessToOriginController alloc] init];
        [success setNavTitle:@"注册完成" desTitle:@"注册完成" btnTitle:@"返回登录"];
        [self.navigationController pushViewController:success animated:YES];
        
        WEAKSELF
        [success setBtnClickHandle:^(SuccessToOriginController *btnVC) {
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }];
    } fail:^(AppError *error) {
        progressHUD.hidden = YES;
        [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
    }];
}

#pragma mark - Private
- (void)p_showSexPicker {
    WEAKSELF
    [BRStringPickerView showStringPickerWithTitle:nil dataSource:kSexArray defaultSelValue:self.sexTextField.text resultBlock:^(id selectValue) {
        if ([NSString app_isBlankString:weakSelf.sexTextField.text] && [NSString app_isBlankString:selectValue]) {
            weakSelf.sexTextField.text = kSexArray[0];
        }else {
            weakSelf.sexTextField.text = selectValue;
        }
    }];
}

- (void)p_showAgePicker {
    WEAKSELF
    [BRStringPickerView showStringPickerWithTitle:nil dataSource:kAgeArray defaultSelValue:self.ageTextField.text resultBlock:^(id selectValue) {
        if ([NSString app_isBlankString:weakSelf.ageTextField.text] && [NSString app_isBlankString:selectValue]) {
            weakSelf.ageTextField.text = kAgeArray[0];
        }else {
            weakSelf.ageTextField.text = selectValue;
        }
    }];
}

- (void)p_showLocationList {
    AddressProvinceCityController *address = [[AddressProvinceCityController alloc] initWithAddressVCType:AddressVCTypeProvince parentId:@"" titleName:@""];
    [self.navigationController pushViewController:address animated:YES];
    
    WEAKSELF
    [address setAddressCompleted:^(NSString *province, NSString *city) {
        weakSelf.locationTextField.text = [NSString stringWithFormat:@"%@ %@", province, city];
        weakSelf.seltedProvince = province;
        weakSelf.seltedCity = city;
    }];
}

- (NSString *)p_validateIsCanRegister {
    if ([NSString app_isBlankString:self.phone] || [NSString app_isBlankString:self.pwd] || [NSString app_isBlankString:self.smsCode]) {
        return @"服务器忙, 稍后再试";
    }
    
    return nil;
}

@end
